package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.DialogUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.model.request.SubmitEmergencyInfoRequest;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.IChangeEmergencyContactPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.ChangeEmergencyContactPresenter;
import cn.sirun.com.fca.personal.center.view.IChangeEmergencyContactView;

public class ChangeEmergencyContactFlowView extends BaseLoadingFlowView<IChangeEmergencyContactPresenter>
        implements IChangeEmergencyContactView {

    //    private TextView mContactTV, mContactPhoneTV;
    private LineEditText mContactET, mContactPhoneET;

    public ChangeEmergencyContactFlowView(Activity activity) {
        super(activity);
    }

    public ChangeEmergencyContactFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();

        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.setting_emergency_contact), ResourcesUtils.getDrawable(R.color.white));
        toolbar.getmBackImageView().setVisibility(View.GONE);
        toolbar.getmBackTextView().setTextColor(ResourcesUtils.getColor(R.color.grey_A9));
        toolbar.setHomeAsUpText(R.string.cancel);
        toolbar.getmMenuTextView().setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
        toolbar.setMenu(R.string.title_save, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleMenuClick(toolbar);
            }
        });
    }

    @Override
    public void onSubmitEmergencyInfoResult(BaseResponse<Void> response) {
        hideLoadingView();

        if (StatusCode.SUCCESS == response.getCode()) {
            getActivity().finish();
            return;
        }

        mContactET.setEnabled(true);
        mContactPhoneET.setEnabled(true);

        DialogUtils.showToast(getActivity(), response.getCode());
    }

    @Override
    protected IChangeEmergencyContactPresenter createPresenter() {
        return new ChangeEmergencyContactPresenter();
    }

    private void handleMenuClick(IovToolbar toolbar) {

        String contract = mContactET.getText().toString();
        String telephone = mContactPhoneET.getText().toString();
        if(!TextUtils.isEmpty(contract) && !TextUtils.isEmpty(telephone)){
            SubmitEmergencyInfoRequest request = new SubmitEmergencyInfoRequest();
            request.setName(contract);
            request.setPhone(telephone);
            getPresenter().submitEmergencyInfo(request);
            showLoadingView();

            mContactET.setEnabled(false);
            mContactPhoneET.setEnabled(false);
        }

    }

    private void initView() {
        setContentView(R.layout.change_emergency_contact_activity);
        final Activity activity = getActivity();
//        mContactTV = activity.findViewById(R.id.tv_contact_value);
//        mContactPhoneTV = activity.findViewById(R.id.tv_contact_phone_value);
        mContactET = activity.findViewById(R.id.et_contact_value);
        mContactPhoneET = activity.findViewById(R.id.et_contact_phone);

        initData();
    }

    private void initData() {
        PersonalInfo info = PersonalInfoManager.getInstance().getPersonalInfo();
        if (info != null) {
            if (!TextUtils.isEmpty(info.getEmergencyContactPerson())) {
                mContactET.setText(info.getEmergencyContactPerson());
            }
            if (!TextUtils.isEmpty(info.getEmergencyContactNumber())) {
                mContactPhoneET.setText(info.getEmergencyContactNumber());
            }
        }
    }


}
